/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Curatorialelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Disposition;
import entityBeans.Preparation;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Identifiedby;
import entityBeans.Recordedby;
import entityBeans.Associatedsequence;
import entityBeans.Typestatus;
import entityBeans.Specimen;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class CuratorialelementJpaController {

    public CuratorialelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Curatorialelement curatorialelement) {
        if (curatorialelement.getPreparationList() == null) {
            curatorialelement.setPreparationList(new ArrayList<Preparation>());
        }
        if (curatorialelement.getIdentifiedbyList() == null) {
            curatorialelement.setIdentifiedbyList(new ArrayList<Identifiedby>());
        }
        if (curatorialelement.getRecordedbyList() == null) {
            curatorialelement.setRecordedbyList(new ArrayList<Recordedby>());
        }
        if (curatorialelement.getAssociatedsequenceList() == null) {
            curatorialelement.setAssociatedsequenceList(new ArrayList<Associatedsequence>());
        }
        if (curatorialelement.getTypestatusList() == null) {
            curatorialelement.setTypestatusList(new ArrayList<Typestatus>());
        }
        if (curatorialelement.getSpecimenList() == null) {
            curatorialelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Disposition disposition = curatorialelement.getDisposition();
            if (disposition != null) {
                disposition = em.getReference(disposition.getClass(), disposition.getIddisposition());
                curatorialelement.setDisposition(disposition);
            }
            List<Preparation> attachedPreparationList = new ArrayList<Preparation>();
            for (Preparation preparationListPreparationToAttach : curatorialelement.getPreparationList()) {
                preparationListPreparationToAttach = em.getReference(preparationListPreparationToAttach.getClass(), preparationListPreparationToAttach.getIdpreparation());
                attachedPreparationList.add(preparationListPreparationToAttach);
            }
            curatorialelement.setPreparationList(attachedPreparationList);
            List<Identifiedby> attachedIdentifiedbyList = new ArrayList<Identifiedby>();
            for (Identifiedby identifiedbyListIdentifiedbyToAttach : curatorialelement.getIdentifiedbyList()) {
                identifiedbyListIdentifiedbyToAttach = em.getReference(identifiedbyListIdentifiedbyToAttach.getClass(), identifiedbyListIdentifiedbyToAttach.getIdidentifiedby());
                attachedIdentifiedbyList.add(identifiedbyListIdentifiedbyToAttach);
            }
            curatorialelement.setIdentifiedbyList(attachedIdentifiedbyList);
            List<Recordedby> attachedRecordedbyList = new ArrayList<Recordedby>();
            for (Recordedby recordedbyListRecordedbyToAttach : curatorialelement.getRecordedbyList()) {
                recordedbyListRecordedbyToAttach = em.getReference(recordedbyListRecordedbyToAttach.getClass(), recordedbyListRecordedbyToAttach.getIdrecordedby());
                attachedRecordedbyList.add(recordedbyListRecordedbyToAttach);
            }
            curatorialelement.setRecordedbyList(attachedRecordedbyList);
            List<Associatedsequence> attachedAssociatedsequenceList = new ArrayList<Associatedsequence>();
            for (Associatedsequence associatedsequenceListAssociatedsequenceToAttach : curatorialelement.getAssociatedsequenceList()) {
                associatedsequenceListAssociatedsequenceToAttach = em.getReference(associatedsequenceListAssociatedsequenceToAttach.getClass(), associatedsequenceListAssociatedsequenceToAttach.getIdassociatedsequence());
                attachedAssociatedsequenceList.add(associatedsequenceListAssociatedsequenceToAttach);
            }
            curatorialelement.setAssociatedsequenceList(attachedAssociatedsequenceList);
            List<Typestatus> attachedTypestatusList = new ArrayList<Typestatus>();
            for (Typestatus typestatusListTypestatusToAttach : curatorialelement.getTypestatusList()) {
                typestatusListTypestatusToAttach = em.getReference(typestatusListTypestatusToAttach.getClass(), typestatusListTypestatusToAttach.getIdtypestatus());
                attachedTypestatusList.add(typestatusListTypestatusToAttach);
            }
            curatorialelement.setTypestatusList(attachedTypestatusList);
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : curatorialelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            curatorialelement.setSpecimenList(attachedSpecimenList);
            em.persist(curatorialelement);
            if (disposition != null) {
                disposition.getCuratorialelementList().add(curatorialelement);
                disposition = em.merge(disposition);
            }
            for (Preparation preparationListPreparation : curatorialelement.getPreparationList()) {
                preparationListPreparation.getCuratorialelementList().add(curatorialelement);
                preparationListPreparation = em.merge(preparationListPreparation);
            }
            for (Identifiedby identifiedbyListIdentifiedby : curatorialelement.getIdentifiedbyList()) {
                identifiedbyListIdentifiedby.getCuratorialelementList().add(curatorialelement);
                identifiedbyListIdentifiedby = em.merge(identifiedbyListIdentifiedby);
            }
            for (Recordedby recordedbyListRecordedby : curatorialelement.getRecordedbyList()) {
                recordedbyListRecordedby.getCuratorialelementList().add(curatorialelement);
                recordedbyListRecordedby = em.merge(recordedbyListRecordedby);
            }
            for (Associatedsequence associatedsequenceListAssociatedsequence : curatorialelement.getAssociatedsequenceList()) {
                associatedsequenceListAssociatedsequence.getCuratorialelementList().add(curatorialelement);
                associatedsequenceListAssociatedsequence = em.merge(associatedsequenceListAssociatedsequence);
            }
            for (Typestatus typestatusListTypestatus : curatorialelement.getTypestatusList()) {
                typestatusListTypestatus.getCuratorialelementList().add(curatorialelement);
                typestatusListTypestatus = em.merge(typestatusListTypestatus);
            }
            for (Specimen specimenListSpecimen : curatorialelement.getSpecimenList()) {
                Curatorialelement oldCuratorialelementOfSpecimenListSpecimen = specimenListSpecimen.getCuratorialelement();
                specimenListSpecimen.setCuratorialelement(curatorialelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldCuratorialelementOfSpecimenListSpecimen != null) {
                    oldCuratorialelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldCuratorialelementOfSpecimenListSpecimen = em.merge(oldCuratorialelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Curatorialelement curatorialelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Curatorialelement persistentCuratorialelement = em.find(Curatorialelement.class, curatorialelement.getIdcuratorialelement());
            Disposition dispositionOld = persistentCuratorialelement.getDisposition();
            Disposition dispositionNew = curatorialelement.getDisposition();
            List<Preparation> preparationListOld = persistentCuratorialelement.getPreparationList();
            List<Preparation> preparationListNew = curatorialelement.getPreparationList();
            List<Identifiedby> identifiedbyListOld = persistentCuratorialelement.getIdentifiedbyList();
            List<Identifiedby> identifiedbyListNew = curatorialelement.getIdentifiedbyList();
            List<Recordedby> recordedbyListOld = persistentCuratorialelement.getRecordedbyList();
            List<Recordedby> recordedbyListNew = curatorialelement.getRecordedbyList();
            List<Associatedsequence> associatedsequenceListOld = persistentCuratorialelement.getAssociatedsequenceList();
            List<Associatedsequence> associatedsequenceListNew = curatorialelement.getAssociatedsequenceList();
            List<Typestatus> typestatusListOld = persistentCuratorialelement.getTypestatusList();
            List<Typestatus> typestatusListNew = curatorialelement.getTypestatusList();
            List<Specimen> specimenListOld = persistentCuratorialelement.getSpecimenList();
            List<Specimen> specimenListNew = curatorialelement.getSpecimenList();
            if (dispositionNew != null) {
                dispositionNew = em.getReference(dispositionNew.getClass(), dispositionNew.getIddisposition());
                curatorialelement.setDisposition(dispositionNew);
            }
            List<Preparation> attachedPreparationListNew = new ArrayList<Preparation>();
            for (Preparation preparationListNewPreparationToAttach : preparationListNew) {
                preparationListNewPreparationToAttach = em.getReference(preparationListNewPreparationToAttach.getClass(), preparationListNewPreparationToAttach.getIdpreparation());
                attachedPreparationListNew.add(preparationListNewPreparationToAttach);
            }
            preparationListNew = attachedPreparationListNew;
            curatorialelement.setPreparationList(preparationListNew);
            List<Identifiedby> attachedIdentifiedbyListNew = new ArrayList<Identifiedby>();
            for (Identifiedby identifiedbyListNewIdentifiedbyToAttach : identifiedbyListNew) {
                identifiedbyListNewIdentifiedbyToAttach = em.getReference(identifiedbyListNewIdentifiedbyToAttach.getClass(), identifiedbyListNewIdentifiedbyToAttach.getIdidentifiedby());
                attachedIdentifiedbyListNew.add(identifiedbyListNewIdentifiedbyToAttach);
            }
            identifiedbyListNew = attachedIdentifiedbyListNew;
            curatorialelement.setIdentifiedbyList(identifiedbyListNew);
            List<Recordedby> attachedRecordedbyListNew = new ArrayList<Recordedby>();
            for (Recordedby recordedbyListNewRecordedbyToAttach : recordedbyListNew) {
                recordedbyListNewRecordedbyToAttach = em.getReference(recordedbyListNewRecordedbyToAttach.getClass(), recordedbyListNewRecordedbyToAttach.getIdrecordedby());
                attachedRecordedbyListNew.add(recordedbyListNewRecordedbyToAttach);
            }
            recordedbyListNew = attachedRecordedbyListNew;
            curatorialelement.setRecordedbyList(recordedbyListNew);
            List<Associatedsequence> attachedAssociatedsequenceListNew = new ArrayList<Associatedsequence>();
            for (Associatedsequence associatedsequenceListNewAssociatedsequenceToAttach : associatedsequenceListNew) {
                associatedsequenceListNewAssociatedsequenceToAttach = em.getReference(associatedsequenceListNewAssociatedsequenceToAttach.getClass(), associatedsequenceListNewAssociatedsequenceToAttach.getIdassociatedsequence());
                attachedAssociatedsequenceListNew.add(associatedsequenceListNewAssociatedsequenceToAttach);
            }
            associatedsequenceListNew = attachedAssociatedsequenceListNew;
            curatorialelement.setAssociatedsequenceList(associatedsequenceListNew);
            List<Typestatus> attachedTypestatusListNew = new ArrayList<Typestatus>();
            for (Typestatus typestatusListNewTypestatusToAttach : typestatusListNew) {
                typestatusListNewTypestatusToAttach = em.getReference(typestatusListNewTypestatusToAttach.getClass(), typestatusListNewTypestatusToAttach.getIdtypestatus());
                attachedTypestatusListNew.add(typestatusListNewTypestatusToAttach);
            }
            typestatusListNew = attachedTypestatusListNew;
            curatorialelement.setTypestatusList(typestatusListNew);
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            curatorialelement.setSpecimenList(specimenListNew);
            curatorialelement = em.merge(curatorialelement);
            if (dispositionOld != null && !dispositionOld.equals(dispositionNew)) {
                dispositionOld.getCuratorialelementList().remove(curatorialelement);
                dispositionOld = em.merge(dispositionOld);
            }
            if (dispositionNew != null && !dispositionNew.equals(dispositionOld)) {
                dispositionNew.getCuratorialelementList().add(curatorialelement);
                dispositionNew = em.merge(dispositionNew);
            }
            for (Preparation preparationListOldPreparation : preparationListOld) {
                if (!preparationListNew.contains(preparationListOldPreparation)) {
                    preparationListOldPreparation.getCuratorialelementList().remove(curatorialelement);
                    preparationListOldPreparation = em.merge(preparationListOldPreparation);
                }
            }
            for (Preparation preparationListNewPreparation : preparationListNew) {
                if (!preparationListOld.contains(preparationListNewPreparation)) {
                    preparationListNewPreparation.getCuratorialelementList().add(curatorialelement);
                    preparationListNewPreparation = em.merge(preparationListNewPreparation);
                }
            }
            for (Identifiedby identifiedbyListOldIdentifiedby : identifiedbyListOld) {
                if (!identifiedbyListNew.contains(identifiedbyListOldIdentifiedby)) {
                    identifiedbyListOldIdentifiedby.getCuratorialelementList().remove(curatorialelement);
                    identifiedbyListOldIdentifiedby = em.merge(identifiedbyListOldIdentifiedby);
                }
            }
            for (Identifiedby identifiedbyListNewIdentifiedby : identifiedbyListNew) {
                if (!identifiedbyListOld.contains(identifiedbyListNewIdentifiedby)) {
                    identifiedbyListNewIdentifiedby.getCuratorialelementList().add(curatorialelement);
                    identifiedbyListNewIdentifiedby = em.merge(identifiedbyListNewIdentifiedby);
                }
            }
            for (Recordedby recordedbyListOldRecordedby : recordedbyListOld) {
                if (!recordedbyListNew.contains(recordedbyListOldRecordedby)) {
                    recordedbyListOldRecordedby.getCuratorialelementList().remove(curatorialelement);
                    recordedbyListOldRecordedby = em.merge(recordedbyListOldRecordedby);
                }
            }
            for (Recordedby recordedbyListNewRecordedby : recordedbyListNew) {
                if (!recordedbyListOld.contains(recordedbyListNewRecordedby)) {
                    recordedbyListNewRecordedby.getCuratorialelementList().add(curatorialelement);
                    recordedbyListNewRecordedby = em.merge(recordedbyListNewRecordedby);
                }
            }
            for (Associatedsequence associatedsequenceListOldAssociatedsequence : associatedsequenceListOld) {
                if (!associatedsequenceListNew.contains(associatedsequenceListOldAssociatedsequence)) {
                    associatedsequenceListOldAssociatedsequence.getCuratorialelementList().remove(curatorialelement);
                    associatedsequenceListOldAssociatedsequence = em.merge(associatedsequenceListOldAssociatedsequence);
                }
            }
            for (Associatedsequence associatedsequenceListNewAssociatedsequence : associatedsequenceListNew) {
                if (!associatedsequenceListOld.contains(associatedsequenceListNewAssociatedsequence)) {
                    associatedsequenceListNewAssociatedsequence.getCuratorialelementList().add(curatorialelement);
                    associatedsequenceListNewAssociatedsequence = em.merge(associatedsequenceListNewAssociatedsequence);
                }
            }
            for (Typestatus typestatusListOldTypestatus : typestatusListOld) {
                if (!typestatusListNew.contains(typestatusListOldTypestatus)) {
                    typestatusListOldTypestatus.getCuratorialelementList().remove(curatorialelement);
                    typestatusListOldTypestatus = em.merge(typestatusListOldTypestatus);
                }
            }
            for (Typestatus typestatusListNewTypestatus : typestatusListNew) {
                if (!typestatusListOld.contains(typestatusListNewTypestatus)) {
                    typestatusListNewTypestatus.getCuratorialelementList().add(curatorialelement);
                    typestatusListNewTypestatus = em.merge(typestatusListNewTypestatus);
                }
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setCuratorialelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Curatorialelement oldCuratorialelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getCuratorialelement();
                    specimenListNewSpecimen.setCuratorialelement(curatorialelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldCuratorialelementOfSpecimenListNewSpecimen != null && !oldCuratorialelementOfSpecimenListNewSpecimen.equals(curatorialelement)) {
                        oldCuratorialelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldCuratorialelementOfSpecimenListNewSpecimen = em.merge(oldCuratorialelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = curatorialelement.getIdcuratorialelement();
                if (findCuratorialelement(id) == null) {
                    throw new NonexistentEntityException("The curatorialelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Curatorialelement curatorialelement;
            try {
                curatorialelement = em.getReference(Curatorialelement.class, id);
                curatorialelement.getIdcuratorialelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The curatorialelement with id " + id + " no longer exists.", enfe);
            }
            Disposition disposition = curatorialelement.getDisposition();
            if (disposition != null) {
                disposition.getCuratorialelementList().remove(curatorialelement);
                disposition = em.merge(disposition);
            }
            List<Preparation> preparationList = curatorialelement.getPreparationList();
            for (Preparation preparationListPreparation : preparationList) {
                preparationListPreparation.getCuratorialelementList().remove(curatorialelement);
                preparationListPreparation = em.merge(preparationListPreparation);
            }
            List<Identifiedby> identifiedbyList = curatorialelement.getIdentifiedbyList();
            for (Identifiedby identifiedbyListIdentifiedby : identifiedbyList) {
                identifiedbyListIdentifiedby.getCuratorialelementList().remove(curatorialelement);
                identifiedbyListIdentifiedby = em.merge(identifiedbyListIdentifiedby);
            }
            List<Recordedby> recordedbyList = curatorialelement.getRecordedbyList();
            for (Recordedby recordedbyListRecordedby : recordedbyList) {
                recordedbyListRecordedby.getCuratorialelementList().remove(curatorialelement);
                recordedbyListRecordedby = em.merge(recordedbyListRecordedby);
            }
            List<Associatedsequence> associatedsequenceList = curatorialelement.getAssociatedsequenceList();
            for (Associatedsequence associatedsequenceListAssociatedsequence : associatedsequenceList) {
                associatedsequenceListAssociatedsequence.getCuratorialelementList().remove(curatorialelement);
                associatedsequenceListAssociatedsequence = em.merge(associatedsequenceListAssociatedsequence);
            }
            List<Typestatus> typestatusList = curatorialelement.getTypestatusList();
            for (Typestatus typestatusListTypestatus : typestatusList) {
                typestatusListTypestatus.getCuratorialelementList().remove(curatorialelement);
                typestatusListTypestatus = em.merge(typestatusListTypestatus);
            }
            List<Specimen> specimenList = curatorialelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setCuratorialelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(curatorialelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Curatorialelement> findCuratorialelementEntities() {
        return findCuratorialelementEntities(true, -1, -1);
    }

    public List<Curatorialelement> findCuratorialelementEntities(int maxResults, int firstResult) {
        return findCuratorialelementEntities(false, maxResults, firstResult);
    }

    private List<Curatorialelement> findCuratorialelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Curatorialelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Curatorialelement findCuratorialelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Curatorialelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getCuratorialelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Curatorialelement> rt = cq.from(Curatorialelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
